package com.health.controller;

import com.health.vo.Result;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 处理所有Controller抛出的异常
 */
@RestControllerAdvice
public class ExceptionController {

    public static final Logger LOGGER = LogManager.getLogger(ExceptionController.class);

    @ExceptionHandler(Exception.class)
    public Object exceptionHandler(Exception e) {
        //打印异常信息到日志中
        LOGGER.error("请求异常", e);

        //封装响应的数据
        return new Result(false, "请求异常：" + e.getMessage());
    }

    @ExceptionHandler(AccessDeniedException.class)
    public Object accessDeniedHandler(Exception e) {
        //封装响应的数据
        return new Result(false, "请求异常：权限不足");
    }
}
